import { requestLogger } from "./log/index";
// import { Message } from "./message/index";
import { reponseConfig, requestConfig } from "./config/index";

function baseRequest(config) {
    return new Promise((resolve, reject) => {
        requestConfig.config(config);
        const {
            url,
            header,
            method,
            data,
            ...other
        } = config;
        uni.request({
            url,
            method,
            header,
            data,
            ...other,
            success(res) {
                if (res.statusCode === 200) {
                    resolve(reponseConfig.response(res));
                }
            },
            complete(response) {
                requestLogger({ url, method, header, data, response });
                if (response.statusCode !== 200) {
                    reject(reponseConfig.error(response));
                }
            }
        });
    })
}

export default baseRequest;